const db = require('../utils/db.js')

const getALlList = (req, res) => {
  const sql = `select * from t_course`
  db.query(sql, (err, results) => {
    console.log(results);
    if (err) return res.cc(err)
    res.ok(results, "获取班级列表成功")
  })
}
const getList = (req, res) => {
  const sql = `select * from t_course where is_delete = 0`
  db.query(sql, (err, results) => {
    console.log(results);
    if (err) return res.cc(err)
    res.ok(results, "获取班级列表成功")
  })
}
const deleteById = (req, res) => {
  const sql = `update t_course set is_delete=? where id=?`
  db.query(sql, [req.params.is_delete, req.params.id], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows == 1) {
      return res.ok(null, "删除课程成功")
    }
    return res.cc("删除失败")
  })
}



// 添加班级
const addCourse = (req, res) => {
  const sql = `select * from t_course where name = ?`
  db.query(sql, req.body.name, (err, results) => {
    if (err) {
      return res.cc(err);
    }
    if (results.length >= 1) {
      return res.cc("该课程名称已存在，请更换课程名称。")
    }
    const sql = `insert into t_course set ?`
    db.query(sql, req.body, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      if (results.affectedRows >= 1) {
        return res.ok(null, "添加课程成功！")
      }
      res.ok(null, "添加课程失败！")
    })
  })
}


module.exports = {
  getALlList, deleteById, addCourse, getList
}